import execjs
# import requests
from curl_cffi import requests
import re
import os
_dir = os.path.dirname(os.path.abspath(__file__))

headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",

}
url = "https://www.adidas.com.cn/"

response = requests.get(url, headers=headers)

print("第一次请求首页")

js_url = 'https://www.adidas.com.cn/' + \
         re.findall('<script type="text/javascript"  src="(.*?)"></script></body>', response.text)[0]
cookies = {
    '_abck': response.cookies.get('_abck'),
    'bm_sz': response.cookies.get('bm_sz'),
    'ak_bmsc': response.cookies.get('ak_bmsc')
}

headers = {
    "referer": "https://www.adidas.com.cn/",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
}
res = requests.get(js_url, headers=headers, cookies=cookies)
# print(res.text)
print("第二次请求获取js", js_url)
cookies['_abck'] = res.cookies.get('_abck')
with open(os.path.join(_dir, 'akamai.js'), encoding='utf-8') as f:
        js_code = f.read().replace('js_code', res.text).replace('cookie_code', cookies['_abck']).replace('script_url',
                                                                                                      js_url)
sensor_data = execjs.compile(js_code).call('get_sensor_data')

data = {'sensor_data': sensor_data}
headers1 = {
    "origin": "https://www.adidas.com.cn",
    "referer": "https://www.adidas.com.cn/",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
}
print(data)
res1 = requests.post(js_url, headers=headers1, cookies=cookies, json=data)
print("第三次请求", res1)
print(res1.cookies)
print(res1.text)
